﻿using System;
using AMM.Business.Store;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.UserControls {
    public partial class InStoreOrderSearchControl : BaseSearchControl {

        public override BaseSearchModel GetSearchModel() {
            int inStoreTypeValue = Convert.ToInt32(this.cmbInStoreType.SelectedValue);
            int storeBasisValue = Convert.ToInt32(this.cmbStoreBasis.SelectedValue);

            InStoreOrderSearchModel searchResult = new InStoreOrderSearchModel();
            searchResult.InStoreOrderCode = this.tbSuspenseOrderCode.Text;
            searchResult.MaterialCode = this.tbMaterialCode.Text;
            searchResult.SuspenseOrderCode = this.tbSuspenseOrderCode.Text;
            searchResult.InStoreDateStart = this.GetDateTimePickerValue(this.dtpInStoreDateStart);
            searchResult.InStoreDateEnd = this.GetDateTimePickerValue(this.dtpInStoreDateEnd);
            searchResult.MaterialInStoreTypeId = inStoreTypeValue;
            searchResult.StoreBasisId = storeBasisValue;

            if (inStoreTypeValue == 0) { searchResult.MaterialInStoreTypeId = null; }
            if (storeBasisValue == 0) { searchResult.StoreBasisId = null; }

            return searchResult;
        }

        public override void ResetSearchModel() {
            this.tbSuspenseOrderCode.Clear();
            this.tbMaterialCode.Clear();
            this.tbInStoreOrderCode.Clear();
            this.dtpInStoreDateStart.Checked = false;
            this.dtpInStoreDateEnd.Checked = false;
            this.cmbInStoreType.SelectedIndex = 0;
            this.cmbStoreBasis.SelectedValue = 0;
        }

        protected void BindStoreBasis() {
            this.cmbStoreBasis.DataSource = new StoreBasisProcessor().FetchEnabledStoreBasis();
            this.cmbStoreBasis.DisplayMember = "Code";
            this.cmbStoreBasis.ValueMember = "StoreBasisId";
        }

        public InStoreOrderSearchControl() {
            InitializeComponent();

            this.BindBasics(this.cmbInStoreType, Business.BasicsType.MaterialInStoreType);
            this.BindStoreBasis();
        }
    }
}
